<?php

namespace Home\Controller;

use Think\Controller;

header('Content-Type:text/html;Charset=UTF-8');

class BaseController extends Controller {

    public $session_id;
    public $cateTrre = array();

    /*
     * 初始化操作
     */

    public function _initialize() {
        $this->session_id = session_id(); // 当前的 session_id
        define('SESSION_ID', $this->session_id); //将当前的session_id保存为常量，供其它方法调用
        // 判断当前用户是否手机

        if (isMobile())
            cookie('is_mobile', '1', 3600);
        else
            cookie('is_mobile', '0', 3600);
        $user = session('user');
        $user = M('users')->where("user_id = {$user['user_id']}")->find();
        session('user', $user);  //覆盖session 中的 user
        $this->user = $user;
        $this->user_id = $user['user_id'];
        $level = M('user_level')->where(array('level_id' => $user['level']))->find(); //会员等级查询

        $this->assign('user', $user); //存储用户信息
        $this->assign('level', $level); //存储用户信息
        $this->assign('user_id', $this->user_id);
        if ($user['is_lock'] == 0) {
            $this->logout();
        }
        unset($_SESSION['tuan_dui']);
        jlsja($user['user_id']); // 查出团队人数
        $this->ztnum = M('users')->where(array('first_leader' => $user['user_id']))->count('user_id'); //查出会员的直推人数

        $this->public_assign();  
        if($user['account'] == ''){
            if($user['mobile'] == ''){
                if($user['email'] != ''){
                    if(tpCache('reg.reg_email_statu') == 1) {
                        $account = $user['email'];
                        $this->fs = 2;
                    }
                }
            }else{
                if(tpCache('reg.reg_phone_statu') == 1) {
                    $account = $user['mobile'];
                    $this->fs = 1;
                }
            }
        }else{
            if(tpCache('reg.reg_account_statu') == 1) {
                $account = $user['account'];
                $this->fs = 3;
            }
        }

        if(tpCache('shop_info.kg') == 0) {
            $this->logout();
        }
        $this->user = $user;
        $this->account = encryptzf($account, 'E', 'zfuwl');
        // $this->account = encryptzf($account, 'E', 'zfuwl');
        $this->tgurl = "http://" . $_SERVER["HTTP_HOST"] . u("Index/reg", array("tjr_accname" =>$this->account)); // 获取推广地址并加密
        $this->tgurl_m = "http://" . $_SERVER["HTTP_HOST"] .'/'. u("Mobile/Index/reg", array("tjr_accname" =>$this->account)); // 获取推广地址并加密
    }
    public function logout() {
        setcookie('uname', '', time() - 3600, '/');
        setcookie('cn', '', time() - 3600, '/');
        setcookie('user_id', '', time() - 3600, '/');
        session_unset();
        session_destroy();
        //$this->success("退出成功",U('Home/Index/index'));
        header("location:" . U('Home/Index/index'));
        exit;
    }

    /**
     * 保存公告变量到 smarty中 比如 导航
     */
    public function public_assign() {

        $tpshop_config = array();
        $tp_config = M('config')->cache(true, TPSHOP_CACHE_TIME)->select();
        foreach ($tp_config as $k => $v) {
            if ($v['name'] == 'hot_keywords') {
                $tpshop_config['hot_keywords'] = explode('|', $v['value']);
            }
            $tpshop_config[$v['inc_type'] . '_' . $v['name']] = $v['value'];
        }

        $goods_category_tree = get_goods_category_tree();
        $this->cateTrre = $goods_category_tree;
        $this->assign('goods_category_tree', $goods_category_tree);
        $brand_list = M('brand')->cache(true, TPSHOP_CACHE_TIME)->field('id,parent_cat_id,logo,is_hot')->where("parent_cat_id>0")->select();
        $this->assign('brand_list', $brand_list);
        $this->assign('tpshop_config', $tpshop_config);
    }

}
